/**
 * Created by xiaobo on 2016/5/24.
 */
;$(function () {
    $('.save_btn ').click(function () {
        var data = $(this).prev();
        var name = data.attr('_name');
        var type = data.attr('_type');
        var value = data.val();
        saveValueAjax(name, value, type);
    });
    
    
    $('#fj_add_btn').click(function () {
        showEditBox();
    });


    $('#submit').click(function () {
        savePrice('add');
    });


    $('.delete').click(function () {
        $(this).parents('.label').remove();
        savePrice('delete');
    });
});


function savePrice(type) {
    if (type == 'add') {
        var input = NANTo0($('#fj_price_input').val());
        if (input==0) {
            layer.msg('请填写价格', {icon: 2});
            return false;
        }
    }
    //获取数据json
    var json = [];
    $('.price').each(function () {
        json.push(NANTo0($(this).text()));
    });

    if (type == 'add') {
        json.push(input);
    }
    saveValueAjax('order_fj_json', JSON.stringify(json), 'string');
}


var index = null;
function showEditBox() {
    $('#fj_price_input').val('');
    index = layer.open({
        type: 1,
        title: '城市上门价格',
        shadeClose: true,
        shade: 0.8,
        area: ['400px', '240px'],
        content: $('#edit-box')
    });
}

function saveValueAjax(name, value, type) {
    var index = layer.load(2);
    $.ajax({
        type: 'POST',
        url: '/admin/sys/saveValue',
        data: {
            'name' : name,
            'value' : value,
            'type' : type
        },
        dataType: 'json',
        success: function (data) {
            if(data.status==1){
                layer.msg(data.info, {icon: 1}, function () {
                    location.reload();
                });
            }else{
                layer.msg(data.info, {icon: 2});
            }
            layer.close(index);
        }
    });
}


function NANTo0(val) {
    val = val ?  val :  0.00;
    val = parseFloat(val);
    return val.toFixed(2);
}